<?php
function home_tutor() {
	global $smarty;
	return $smarty->fetch("home_tutor.html");
}
function process_tutor() {

global $smarty, $function;
	$a = isset($_GET['a']) ? $_GET['a'] : "";
	$oTutor = new Tutor(); // class database of Tutor
	$oStudent = new Student();
	switch(strtolower($a)) {
	
		default:
		case "manage":
			$choose = isset($_GET['p']) ? strtolower($_GET['p']) : "";
			if($choose == "module") {
				$list_students_attemps = $oTutor->getStudentAttemps(2);
				$smarty->assign("list_students_attemps", $list_students_attemps);
				$smarty->assign("choose", 1);
			} elseif($choose == "unit") {
				$list_students_attemps = $oTutor->getStudentAttemps(1);
				$smarty->assign("list_students_attemps", $list_students_attemps);
				$smarty->assign("choose", 1);
			} else {
				$smarty->assign("choose", 0);
			}		
			return $smarty->fetch("tutor/attemplist.html"); 
		break;
		
		case "editattempt":		
			if(isset($_POST['btn_submit'])) {
				$number_questions = intval($_POST['number_questions']);	
				$testid = $_POST['testid'];					
				$attempt = intval($_POST['attempt']);	
				$questionid = $_POST['qid'];	
				$stuid =$_POST['stuid'];				
				for($i=1; $i <= $number_questions; $i++) {
					$markName = "mark".$i;
					$mark[$i] = $_POST[$markName];
					
					$feebackName = "feedback".$i;
					$feeback[$i] = $_POST[$feebackName];
					$feeback[$i] = stripslashes($function->FixQuotes($feeback[$i]));
					
					$questionNo = $questionid[$i-1];
					$oTutor->updateMarkAndFeedback($stuid,$testid,$questionNo,$attempt,$mark[$i],$feeback[$i]);					
				}
				$oTutor->setFeedbackStatus($stuid,$testid,$attempt);
				$overall_mark = $oStudent->sumMarks($testid, $stuid, $attempt);
				$oTutor->setOverallMark($stuid,$testid,$attempt,$overall_mark);
				return $function->msg_box("Mark and Feedback are confirmed.",2,"?mod=markattempt");
			}else{
				$testid =$_GET['testid'];
				$stuid =$_GET['stuid'];
				$done_test = $oStudent->check_do_test($testid, $stuid);		
				
				if($done_test['Attempt'] != null) {
					$list_questions = $oStudent->getAllQuestionsAnswerInTest($testid, $stuid, $done_test['Attempt']);
					$number_questions = count($list_questions);
					$smarty->assign("list_questions", $list_questions);
					$smarty->assign("stuid", $stuid);
					$smarty->assign("testid", $testid);
					$smarty->assign("attempt", $done_test['Attempt']);
					$overall_mark = $oStudent->sumMarks($testid, $stuid, $done_test['Attempt']);
					$smarty->assign("overall_mark", $overall_mark);
					$smarty->assign("number_questions", $number_questions);					
				}
				return $smarty->fetch("tutor/markstudentattemp.html"); 

			}			
		break;

		case "readytest":
			$choose = isset($_GET['p']) ? strtolower($_GET['p']) : "";
			if($choose == "module") {
				$list = $oTutor->showAllReadyTests(2);
				$smarty->assign("list_ready_tests", $list);
				$smarty->assign("choose", 1);
			} elseif($choose == "unit") {
				$list = $oTutor->showAllReadyTests(1);
				$smarty->assign("list_ready_tests", $list);
				$smarty->assign("choose", 1);
			} else {
				$smarty->assign("choose", 0);
			}
			return $smarty->fetch("tutor/readytest.html");
		break;
		
		case "nreadytest":
			$choose = isset($_GET['p']) ? strtolower($_GET['p']) : "";
			if($choose == "module") {
				$list = $oTutor->showAllNReadyTests(2);
			$smarty->assign("list_nready_tests", $list);
				$smarty->assign("choose", 1);
			} elseif($choose == "unit") {
				$list = $oTutor->showAllNReadyTests(1);
			$smarty->assign("list_nready_tests", $list);
				$smarty->assign("choose", 1);
			} else {
				$smarty->assign("choose", 0);
			}
			return $smarty->fetch("tutor/notreadytest.html");
		break;
		
		case "allowready":
			if(isset($_POST['btn_allowready'])) {
				$checked = $_POST['check_ready'];
				foreach($checked as $checked_id) {
					$checked_id = intval($checked_id);	
					$oTutor->doTestReady($checked_id);
				}
				return $function->msg_box("Success!!!", 2, "?mod=htutor");
			} else {
				return $function->msg_box("Error in processing. Please try again later.", 2, "?mod=markattempt&a=nreadytest");
			}
		break;
		
	}

}

?>